package com.tongfusoft.document.manager.profile.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.da.template.service.impl.DataTemplateServiceImpl;
import com.tongfusoft.document.entity.FieldsetHasMetas;
import com.tongfusoft.document.entity.InputProfiles;
import com.tongfusoft.document.entity.IpFieldsets;
import com.tongfusoft.document.entity.MetadataDefine;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.profile.service.InputProfileService;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.utils.UIDGeneratorService;

@Service("profileService")
public class InputProfileServiceImpl extends DataTemplateServiceImpl implements
		InputProfileService {

	@Autowired
	@Qualifier("profileService")
	public InputProfileService inputProfileService;

	@Override
	public void createProfile(InputProfiles profile, Users user) {

		if (this.selectList("getProfileByName", profile).size() > 0) {

			throw new ServiceException("repeat_error");
		}
		profile.setProfileId(UIDGeneratorService.getUUID());
		profile.setCreatedBy(user.getUserId());
		profile.setCreatedDate(new Timestamp(new Date().getTime()));
		profile.setLastUpdBy(user.getUserId());
		profile.setLastUpdDate(new Timestamp(new Date().getTime()));
		profile.setStatus("1");
		profile.setIsEnable("0");
		this.insert("createProfile", profile);

	}

	@Override
	public void updateProfile(String profileId,InputProfiles inputProfiles, Users user) {
		
		
		InputProfiles searchProfiles = new InputProfiles();
		searchProfiles.setProfileId(profileId);
		
		InputProfiles profiles = this.inputProfileService.getInputProfiles(searchProfiles);
		
		profiles.setProfileName(inputProfiles.getProfileName());
		profiles.setProfileNote(inputProfiles.getProfileNote());
		profiles.setLastUpdBy(user.getUserId());
		profiles.setLastUpdDate(new Timestamp(new Date().getTime()));
		
		if (this.selectList("getProfileByNameOutSelf", profiles).size() > 0) {

			throw new ServiceException("repeat_error");
		}
		
		this.update("updateProfile", profiles);

	}

	@Override
	public void deleteProfile(InputProfiles profile, Users user) {

		InputProfiles delProfiles = this.inputProfileService
				.getInputProfiles(profile);
		delProfiles.setLastUpdBy(user.getUserId());
		delProfiles.setLastUpdDate(new Timestamp(new Date().getTime()));
		this.delete("deleteProfile", delProfiles);

	}

	@Override
	public void addFieldSets(IpFieldsets fieldset, Users user) {
		if (this.selectList("getIpFieldsetByName", fieldset).size() > 0) {

			throw new ServiceException("repeat_error");
		}

		fieldset.setPfId(UIDGeneratorService.getUUID());
		fieldset.setFieldsetId(UIDGeneratorService.getUUID());
		fieldset.setStatus("1");
		fieldset.setCreatedBy(user.getUserId());
		fieldset.setCreatedDate(new Timestamp(new Date().getTime()));
		fieldset.setLastUpdBy(user.getUserId());
		fieldset.setLastUpdDate(new Timestamp(new Date().getTime()));
		this.insert("addFieldset", fieldset);

	}

	@Override
	public void updateFieldSet(IpFieldsets fieldset, Users user) {
		this.update("updateFieldset", fieldset);

	}

	@Override
	public void deleteFieldSets(IpFieldsets fieldset, Users user) {

		FieldsetHasMetas fieldsetHasMetas = new FieldsetHasMetas();
		fieldsetHasMetas.setFieldsetId(fieldset.getFieldsetId());
		List<FieldsetHasMetas> fieldsetHasMetasList = this.inputProfileService
				.getMetaDataIdList(fieldsetHasMetas);

		FieldsetHasMetas fieldsetHas = new FieldsetHasMetas();
		fieldsetHas.setFieldsetId(fieldset.getFieldsetId());
		for (FieldsetHasMetas fieldsetHasMetas2 : fieldsetHasMetasList) {

			fieldsetHas.setMetadataId(fieldsetHasMetas2.getMetadataId());

			this.inputProfileService.deleteMetaDataToFieldSet(fieldsetHas, user);
		}

		fieldset.setLastUpdBy(user.getUserId());
		fieldset.setLastUpdDate(new Timestamp(new Date().getTime()));
		this.delete("deleteFieldset", fieldset);

	}

	@Override
	public void addMetaDataToFieldSet(FieldsetHasMetas metaData, Users user) {
		metaData.setFmId(UIDGeneratorService.getUUID());
		metaData.setStatus("1");
		metaData.setCreatedBy(user.getUserId());
		metaData.setCreatedDate(new Timestamp(new Date().getTime()));
		metaData.setLastUpdBy(user.getUserId());
		metaData.setLastUpdDate(new Timestamp(new Date().getTime()));
		this.insert("addMetadataToFieldset", metaData);

	}

	@Override
	public void deleteMetaDataToFieldSet(FieldsetHasMetas metaData, Users user) {
		metaData.setLastUpdBy(user.getUserId());
		metaData.setLastUpdDate(new Timestamp(new Date().getTime()));
		this.delete("deleteMetadataToFieldset", metaData);

	}

	@Override
	public PageInfo profileList(int pageSize, int pageIndex,
			InputProfiles profile) {
		return super.findPage("profileList", profile, pageSize, pageIndex);
	}

	@Override
	public List<IpFieldsets> FieldSetList(IpFieldsets fieldset) {
		List<IpFieldsets> list = this.selectList("fieldsetList", fieldset);
		return list;
	}

	@Override
	public List<MetadataDefine> metaDataList(InputProfiles profile) {
		return this.selectList("metadataList", profile);
	}

	@Override
	public void updateEnableProfile(InputProfiles profile, String isEnable,
			Users user) {

		InputProfiles delProfiles = this.inputProfileService
				.getInputProfiles(profile);

		delProfiles.setIsEnable(isEnable);
		delProfiles.setLastUpdBy(user.getUserId());
		delProfiles.setLastUpdDate(new Timestamp(new Date().getTime()));

		this.update("updateEnableProfile", delProfiles);

	}

	@Override
	public InputProfiles getInputProfiles(InputProfiles profile) {
		InputProfiles inputProfiles = (InputProfiles) this.selectOne(
				"getInputProfile", profile);
		return inputProfiles;
	}

	@Override
	public IpFieldsets getIpFieldsets(IpFieldsets ipFieldsets) {
		return (IpFieldsets) this.selectOne("getIpFieldset", ipFieldsets);
	}

	@Override
	public FieldsetHasMetas getFieldsetHasMetas(
			FieldsetHasMetas fieldsetHasMetas) {
		return (FieldsetHasMetas) this.selectOne("getFieldsetHasMetadata",
				fieldsetHasMetas);
	}

	@Override
	public List<FieldsetHasMetas> getMetaDataIdList(
			FieldsetHasMetas fieldsetHasMetas) {

		return this.selectList("getMetadataIdList", fieldsetHasMetas);
	}

	@Override
	public List<FieldsetHasMetas> getMetaDataIdByProfileId(InputProfiles profile) {
		return this.selectList("getMetadataIdByProfileId", profile);
	}

	@Override
	public InputProfiles getInputProfilesByFieldSetId(IpFieldsets ipFieldsets) {
		return (InputProfiles) this.selectOne("getInputProfilesByFieldsetId",
				ipFieldsets);
	}

	@Override
	public List<MetadataDefine> metaDataListByFieldSet(IpFieldsets ipFieldsets) {

		return this.selectList("metadataListByFieldset", ipFieldsets);
	}

	@Override
	public void updateMetaDataToFieldSet(FieldsetHasMetas metaData, Users user) {
		metaData.setLastUpdBy(user.getUserId());
		metaData.setLastUpdDate(new Timestamp(new Date().getTime()));
		this.update("updateMetadataToFieldset", metaData);
	}

	@Override
	public List<MetadataDefine> fieldsetHasMetadata(IpFieldsets ipFieldsets) {
		return this.selectList("fieldsetHasMetadata", ipFieldsets);
	}

	@Override
	public List<InputProfiles> validProfile() {
		// TODO Auto-generated method stub
		return this.selectList("validProfileList");
	}

	
	@Override
	public void addFieldSetMetada(String metadataIdList, String metaSortList,
			String isRequireList, String fieldsetId, Users user) {
		
		String[] metadataList = metadataIdList.split(",");
		
		String[] metaSortValList = metaSortList.split(",");
		
		String[] isRequireValList = isRequireList.split(",");
		
		FieldsetHasMetas fieldsetHasMetas = new FieldsetHasMetas();	
		
	   for (int i = 0; i < metadataList.length; i++) {
			if(metadataList[i].equals("") && metaSortValList[i].equals("") && isRequireValList[i].equals("")){
			}else{
				fieldsetHasMetas.setFieldsetId(fieldsetId);
				fieldsetHasMetas.setMetadataId(metadataList[i]);
				FieldsetHasMetas isMetadata = this.inputProfileService.getFieldsetHasMetas(fieldsetHasMetas);
				if(isMetadata!=null){
					this.inputProfileService.updateMetaDataToFieldSet(isMetadata, user);
				}else{
					fieldsetHasMetas.setMetaSort(Integer.valueOf(metaSortValList[i]));
					fieldsetHasMetas.setIsRequire(isRequireValList[i]);
					
					this.inputProfileService.addMetaDataToFieldSet(fieldsetHasMetas, user);
				}
			}
		}

	}
	
	@Override
	public void updateFieldSetMetadata(String metadataIdList, String metaSortList,
			String isRequireList, String fieldsetId, Users user) {
		
		String[] metadataList = metadataIdList.split(",");
		String[] metaSort = metaSortList.split(",");
		String[] isRequire = isRequireList.split(",");
		
		FieldsetHasMetas fieldsetHasMetas = new FieldsetHasMetas();
		
		for (int i = 0; i < metadataList.length; i++) {
			if(metadataList[i].equals("") || metadataList[i] == null){
			}else{
				fieldsetHasMetas.setFieldsetId(fieldsetId);
				fieldsetHasMetas.setMetadataId(metadataList[i]);
				FieldsetHasMetas fieldsetHasMetasUp = this.inputProfileService.getFieldsetHasMetas(fieldsetHasMetas);					
				fieldsetHasMetasUp.setIsRequire(isRequire[i]);
				fieldsetHasMetasUp.setMetaSort(Integer.valueOf(metaSort[i]));
				this.inputProfileService.updateMetaDataToFieldSet(fieldsetHasMetasUp, user);
			}
		}
		
	}

}
